package com.atguigu.lease.web.admin.controller.lease;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ViewAppointment;
import com.atguigu.lease.model.enums.AppointmentStatus;
import com.atguigu.lease.web.admin.service.ViewAppointmentService;
import com.atguigu.lease.web.admin.vo.appointment.AppointmentQueryVo;
import com.atguigu.lease.web.admin.vo.appointment.AppointmentVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "预约看房管理")
@RequestMapping("/admin/appointment")
@RestController
public class ViewAppointmentController {
    @Autowired
    private ViewAppointmentService viewAppointmentService;

    //todo:注意1：本次条件需要添加在where后面，子表条件需要筛主表
    //todo：注意2：日期格式问题：方案一：jackson得注解@Jsonformat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8)
    //todo"方案二：全局配置 application.yml 里面spring.jackson.date-format || date-format
    @Operation(summary = "分页查询预约信息")
    @GetMapping("page")
    public Result<IPage<AppointmentVo>> page(@RequestParam long current, @RequestParam long size, AppointmentQueryVo queryVo) {
        //1.封装page对象
        Page<AppointmentVo> page = new Page<>(current,size);
        //2.调用业务方法
        viewAppointmentService.customPage(page,queryVo);
        //mybatis-plus会对我们传入的对象进行完善！ page1（current,size) -> service -> mapper -> mybatis-plus ->
        //  page 1(recorders,total)
        return Result.ok(page);
    }

    @Operation(summary = "根据id更新预约状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam AppointmentStatus status) {
        LambdaUpdateWrapper<ViewAppointment> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ViewAppointment::getId,id);
        lambdaUpdateWrapper.set(ViewAppointment::getAppointmentStatus,status);
        viewAppointmentService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

}
